package com.hl.card.award.controller;

import com.alibaba.fastjson.JSONObject;
import com.hl.card.award.mq.Mq4NotifyDemo;
import com.hl.card.award.mq.MqCustomConfig;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.award.service.IRewardByTrans;
import com.hl.card.modular.pack.service.UserDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * java类简单作用描述
 *
 * @ProjectName: distdev
 * @Package: com.hl.card.modular.pack.controller
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 作者姓名
 * @CreateDate: 2018/7/9 17:45
 * @UpdateUser: Neil.Zhou
 * @UpdateDate: 2018/7/9 17:45
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * <p>Copyright: Copyright (c) 2018</p>
 */
@RestController
@RequestMapping("/api")
public class RewardByCountController {
    @Autowired
    private Mq4NotifyDemo mq4NotifyDemo;
    @Autowired
    private IRewardByTrans rewardByTrans;
    @Autowired
    private UserDataService userDataService;
    @PostMapping(value = "/rewardByCount")
    public ResponseObject rewardByCount(String userId, String prodCode, String amount, int count) {
        return rewardByTrans.makeDistProfitSplitByCount(userId, prodCode, amount, count);
    }

    @PostMapping(value = "/rewardByBatch")
    public ResponseObject rewardByBatch(String userId, String prodCode, String amount, int count, String mchOrderNo) {
        return rewardByTrans.makeDistProfitSplitByBatch(userId, prodCode, amount, count, mchOrderNo);
    }

    @PostMapping(value = "/rewardWxCommunity")
    public ResponseObject rewardWxCommunity(String communityAdmin, String recommender, String userId, String amount) {
        return rewardByTrans.makeDistProfitSplitByWxCommunity(communityAdmin, recommender, userId, amount);
    }

    @RequestMapping(value = "/makeCommunityAward")
    public ResponseObject makeCommunityAward(String parentCode, String userId, String amount, String orderId) {
        return rewardByTrans.makeCommunityAward(parentCode, userId, amount, orderId);
    }

    @RequestMapping(value = "/makeAwardByCount")
    public ResponseObject makeAwardByCount(String prodCode, String userId, String amount, String orderId, int count) {
        return rewardByTrans.makeAwardByCount(prodCode, userId, amount, orderId, count);
    }

    @RequestMapping(value = "/makeAward")
    public ResponseObject makeAward(String prodCode, String userId, String amount, String orderId) {
        return rewardByTrans.makeAward(prodCode, userId, amount, orderId);
    }

    @RequestMapping(value = "/sendMakeAward")
    public ResponseObject sendMakeAward(String prodCode, String userId, String amount, String orderId) {
        JSONObject json = new JSONObject();
        json.put("parentCode", prodCode);
        json.put("userId", userId);
        json.put("amount", amount);
        json.put("orderId", orderId);
        json.put("function", "makeAward");
        mq4NotifyDemo.send(MqCustomConfig.QUEUE_AWARD, json.toJSONString());
        ResponseObject res = new ResponseObject();
        res.setErroCode(0);
        res.setData("发送成功");
        return res;
    }

    @RequestMapping(value = "/makeAwardManualUnfreeze")
    public ResponseObject makeAwardManualUnfreeze(String prodCode, String userId, String amount, String orderId, String unfreezeTime) {
        return rewardByTrans.makeAward(prodCode, userId, amount, orderId, unfreezeTime);
    }

    @RequestMapping(value = "/makeAdvanceAward")
    public ResponseObject makeAdvanceAward(String prodCode, String regCode, String userId, String amount, String orderId) {
        return rewardByTrans.makeAdvanceAward(prodCode, regCode, userId, amount, orderId);
    }

    @RequestMapping(value = "/makeAdvanceAwardManualUnfreeze")
    public ResponseObject makeAdvanceAwardManualUnfreeze(String prodCode, String regCode, String userId, String amount, String orderId, String unfreezeTime) {
        return rewardByTrans.makeAdvanceAward(prodCode, regCode, userId, amount, orderId, unfreezeTime);
    }

    @RequestMapping(value = "/reConfirmCommunityAward")
    public ResponseObject reConfirmCommunityAward(String userId) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(0);
        res.setData(rewardByTrans.confirmCommunityAward(userId));
        return res;
    }

    @RequestMapping(value = "/confirmAuthAward")
    public ResponseObject confirmAuthAward(String orderId) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(0);
        res.setData(rewardByTrans.confirmAuthAward(orderId));
        return res;
    }

    @RequestMapping(value = "/manualUnfreezeAward")
    public ResponseObject manualUnfreezeAward(String userId, String mchOrderNo, String amount) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(0);
        res.setData(rewardByTrans.manualUnfreezeAward(userId, mchOrderNo, amount));
        return res;
    }

    @RequestMapping(value = "/commonReward")
    public ResponseObject commonReward(String prodCode, String orderId, String parentRecommender, String recommender, String userId, String amount, String unfreezeTime) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(0);
        res.setData(rewardByTrans.makeDistProfit(prodCode, orderId, parentRecommender, recommender, userId, amount, unfreezeTime));
        return res;
    }

    /**
     * 获取兑换面值列表
     */
    @RequestMapping(value = "/award/getRate", method = RequestMethod.GET)
    public ResponseObject getAwardRate(String prodCode) {
        ResponseObject res = new ResponseObject();
        res.setErroCode(1);
        Map map= (Map) userDataService.fractionRateV1(prodCode);
        if(0 == (Integer)(map.get("code"))) {
            res.setErroCode(0);
        }
        res.setData(map);
        return res;
    }
}
